package com.dstz.form.util.tableexcel.column;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dstz.bus.api.constant.BusColumnCtrlType;
import com.dstz.bus.api.model.IBusinessColumn;
import com.dstz.sys.api.model.dto.DataDictDTO;
import com.dstz.sys.api.service.DataDictService;

import cn.hutool.core.util.StrUtil;

/**
 * <pre>
 * 数据字典
 * </pre>
 * 
 * @author aschs
 * @date 2019年9月9日
 * @owner 大世同舟
 */
@Service
public class DicExcellor extends BusinessColumnExcellor {
	@Autowired
	private DataDictService dataDictService;

	@Override
	BusColumnCtrlType getCtrlType() {
		return BusColumnCtrlType.DIC;
	}

	@Override
	Object fromExcel(IBusinessColumn column, Object text, JSONObject config) {
		String[] ts = text.toString().split(",");
		List<String> ls = new ArrayList<>();

		List<DataDictDTO> dictNodeList = dataDictService.getDictNodeList(config.getString("key"), false);
		for (String t : ts) {
			boolean exist = false;
			for (DataDictDTO dto : dictNodeList) {
				if (dto.getName().equals(t)) {
					exist = true;
					ls.add(dto.getKey());
				}
			}
			if (!exist) {
				ls.add(t);
			}
		}

		return StrUtil.join(",", ls.toArray());
	}

	@Override
	Object toExcel(IBusinessColumn column, Object value, JSONObject config) {
		String[] vs = value.toString().split(",");
		List<String> ls = new ArrayList<>();

		List<DataDictDTO> dictNodeList = dataDictService.getDictNodeList(config.getString("key"), false);
		for (String v : vs) {
			boolean exist = false;
			for (DataDictDTO dto : dictNodeList) {
				if (dto.getKey().equals(v)) {
					exist = true;
					ls.add(dto.getName());
				}
			}
			if (!exist) {
				ls.add(v);
			}
		}

		return StrUtil.join(",", ls.toArray());
	}

	
}
